#import "SMRShadowSubtleScrollView.h"
#import "SMROverlayShadowEffectView.h"
#import "NSObject+BlockObservation.h"


#pragma mark Constants


#pragma mark -
#pragma mark Class Globals


#pragma mark -
@interface SMRShadowSubtleScrollView ()
@property (nonatomic, assign) BOOL overlayInnerRectNeedsUpdate;
@property (nonatomic, assign) NSRect overlayInnerRect;
@property (nonatomic, assign) BOOL wasSelectedBeforeEditing;
@property (nonatomic, assign) BOOL editing;
@end


#pragma mark -
@interface SMRShadowSubtleScrollView (SMRShadowSubtleScrollViewPrivateMethods)
-(void)shadowSubtleScrollViewCommonInit_;
@end


#pragma mark -
@implementation SMRShadowSubtleScrollView

@synthesize overlayInnerRectNeedsUpdate;
@synthesize overlayInnerRect;
@synthesize shadowView;
@synthesize wasSelectedBeforeEditing;
@synthesize editing;


-(id)initWithFrame:(NSRect)frameRect {
	if (!(self = [super initWithFrame:frameRect]))
		return nil;
	[self shadowSubtleScrollViewCommonInit_];
	return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return nil;
	[self shadowSubtleScrollViewCommonInit_];
	return self;
}

-(void)shadowSubtleScrollViewCommonInit_ {
	[self setShadowView:[[SMROverlayShadowEffectView alloc] initWithFrame:NSZeroRect]];
	[[self shadowView] setBorderPadding:4.0f];
    NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
	[noteCenter addObserver:self selector:@selector(scrollViewFrameDidChange:) name:NSViewFrameDidChangeNotification object:self];
}


#pragma mark -
#pragma mark Notifications

-(void)scrollViewFrameDidChange:(NSNotification *)note {
	if ([self editing])
		[self updateOverlay];
}

#pragma mark -
#pragma mark Drawing

-(void)drawRect:(NSRect)dirtyRect {
	if (![self editing]) {
		[super drawRect:dirtyRect];
		return;
	}
	[[NSColor whiteColor] setFill];
	[NSBezierPath fillRect:[self overlayInnerRect]];
}


#pragma mark -
#pragma mark Overrides

-(void)viewWillDraw {
	if ([self overlayInnerRectNeedsUpdate])
		[self updateOverlay];
}

-(void)viewDidMoveToWindow {
	AMBlockTask firstResponderTask = ^(id object, NSDictionary *change) {
		NSResponder *documentView = [self documentView];
		NSResponder *currentFirstResponder = [[self window] firstResponder];
		if (currentFirstResponder == documentView) {
			[self showEditModeBezel];
			[self setWasSelectedBeforeEditing:[self selected]];
			[self setSelected:NO];
			[self setEditing:YES];
			return;
		}
		NSResponder *previousFirstResponder = [change valueForKey:NSKeyValueChangeOldKey];
		if (previousFirstResponder != documentView)
			return;
		[self hideEditModeBezel];
		[self setSelected:[self wasSelectedBeforeEditing]];
		[self setEditing:NO];
	};
	[[self window] addObserverForKeyPath:@"firstResponder" options:NSKeyValueObservingOptionOld task:firstResponderTask];
}


#pragma mark -
#pragma mark Overlay support

-(void)showEditModeBezel {
	[self setOverlayInnerRectNeedsUpdate:YES];
	[self setNeedsDisplay:YES];
}

-(void)hideEditModeBezel {
	[[self shadowView] removeFromSuperview];
	[self setNeedsDisplay:YES];
}

-(NSRect)overlayRectForRect:(NSRect)bounds {
	return bounds;
}

-(void)updateOverlay {
	NSRect overlayRect = [self overlayRectForRect:[self visibleRect]];
	[self setOverlayInnerRect:overlayRect];
	[self setOverlayInnerRectNeedsUpdate:NO];
	
	if  ([[self shadowView] superview]) 
		[[self shadowView] updateOverlayRect:overlayRect];
	else 
		[[self shadowView] setOverlayRect:overlayRect inView:self];
}


@end
